<template>
  <div>
    <Head />
    <div class="User">
      <div class="User_right">
        <AuthorInformation :user="user" :title="'个人信息'" />
        <FriendshipCommunity />
        <ClientQrCode />
      </div>
      <div class="User_left">
        <div class="User_left_user">
          <p class="User_left_top" style="font-size: 14px; color: #999">
            <router-link to="/"
              ><span class="User_left_top_span">主页</span></router-link
            >
            &nbsp;/ &nbsp;
          </p>
          <div class="User_title">
            <div style="height: 40px; margin-bottom: 8px">
              <img
                v-if="user.id != tokenUser.id"
                class="User_left_avatar"
                :src="user.avatar"
                alt="头像"
              />
              <el-upload
                v-else
                class="avatar-uploader User_left_avatar"
                :action="action"
                :headers="token"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload"
                :on-error="headError"
              >
                <img :src="user.avatar" class="avatar User_left_avatar" />
              </el-upload>
              <p class="User_left_name">{{ user.username }}</p>
            </div>
            <p style="color: #333">{{ integral }} 积分</p>
            <p class="User_left_name_githud">
              <img src="../../public/img/githud.jpg" alt="" /> @{{
                user.username
              }}
            </p>
            <p style="color: #a3a3a3">
              注册时间{{ analysisTime(user.created_at) }}
            </p>
          </div>
        </div>
        <div class="User_left_establish">
          <p class="User_left_top" style="font-size: 14px; color: #000">
            最近创建的话题
          </p>
          <div
            class="User_title"
            style="padding: 0px"
            v-if="article.articles.length"
          >
            <ArticleList :list="article.articles" :querys="'*'" />

            <router-link to="#"
              ><p class="User_more">查看更多>></p></router-link
            >
          </div>
          <p v-else class="User_no_article">无话题</p>
        </div>
        <div class="User_left_establish">
          <p class="User_left_top" style="font-size: 14px; color: #000">
            最近参与的话题
          </p>
          <div class="User_title" style="padding: 0px" v-if="replie.length">
            <ArticleList :list="replie" :querys="'*'" />

            <router-link to="#"
              ><p class="User_more">查看更多>></p></router-link
            >
          </div>
          <p v-else class="User_no_article">无话题</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Head from "@/components/Head.vue"; //头部
import AuthorInformation from "@/components/AuthorInformation.vue"; //作者信息
import ArticleList from "@/components/ArticleList.vue"; //话题列表
import FriendshipCommunity from "@/components/FriendshipCommunity.vue"; //友情社区
import ClientQrCode from "@/components/ClientQrCode.vue"; //客户端二维码
export default {
  name: "User",
  components: {
    Head,
    AuthorInformation,
    ArticleList,
    FriendshipCommunity,
    ClientQrCode,
  },
  data() {
    return {
      user: {},
      tokenUser: {},
      action:
        process.env.VUE_APP_URL + "/api/upload/picture/token?url=head_img", //图片上传地址
      token: { authorization: localStorage.getItem("authorization") }, //请求头
      authorization: localStorage.getItem("authorization"), //token
      article: {
        articles: [],
      },
      replie: [],
      integral: 0, //积分
      label: "“这个人很懒，什么个性签名都没有留下来。”", //标签
    };
  },
  methods: {
    getUser() {
      this.axios
        .get("/get/user?username=" + this.$route.params.username)
        .then((res) => {
          // console.log(res.data);
          this.user = res.data.results;
        })
        .catch((error) => {
          console.log(error);
          this.$message.error(error.message);
        });
    },
    getUserToken() {
      if (this.authorization) {
        this.axios
          .get("/get/user/token")
          .then((res) => {
            this.tokenUser = res.data.results;
          })
          .catch((error) => {
            console.log(error);
            localStorage.setItem("authorization", "");
          });
      }
    },
    handleAvatarSuccess(res) {
      console.log(res);
      if (res.results.code == 200) {
        this.user.avatar = res.results.url;
      } else {
        this.$message.error(res.results.message);
      }
    },
    headError(error) {
      console.log(error);
      this.$message.error("上传错误！");
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    getUserArticle() {
      //最新创建话题
      this.axios
        .get("/get/user/name/article?username=" + this.$route.params.username)
        .then((res) => {
          // console.log(res.data);
          this.article = res.data.results.user;
        })
        .catch((error) => {
          console.log(error);
          this.$message.error(error.message);
        });
    },
    getUserReplie() {
      //最近参与话题
      this.axios
        .get("/get/user/name/replie?username=" + this.$route.params.username)
        .then((res) => {
          console.log(res.data);
          let arr = [];
          for (let i = 0; i < res.data.results.user.replies.length; i++) {
            arr.push(res.data.results.user.replies[i].article);
          }
          this.replie = arr;
        })
        .catch((error) => {
          console.log(error);
          this.$message.error(error.message);
        });
    },
    analysisTime(time) {
      const _d1 = new Date();
      const dateDiffer = Math.abs(_d1.getTime() - time);
      //准换年
      const year = Math.floor(dateDiffer / (1000 * 60 * 60 * 24 * 365));
      //取余数
      const years = dateDiffer % (1000 * 60 * 60 * 24 * 365);
      //将余数转换成月
      const month = Math.floor(years / (1000 * 60 * 60 * 24 * 30));
      //将余数转换成天
      const day = Math.floor(years / (1000 * 60 * 60 * 24));
      //取余数
      const hours = years % (1000 * 60 * 60 * 24);
      //将余数转换小时
      const hour = Math.floor(hours / (1000 * 60 * 60));
      // console.log(year,month,day,hour);
      if (year > 0) {
        return year + "年前";
      } else if (month > 0) {
        return month + "个月前";
      } else if (day > 0) {
        return day + "天前";
      } else if (hour > 0) {
        return hour + "小时前";
      } else {
        return "1小时之前";
      }
    },
  },
  mounted() {
    this.getUser();
    this.getUserArticle();
    this.getUserReplie();
    this.getUserToken();
  },
};
</script>

<style>
.User {
  width: 90%;
  margin: auto;
  padding-top: 26px;
}
.User_left {
  margin-right: 305px;
}
.User_right {
  float: right;
  width: 290px;
  padding-bottom: 20px;
}
.User_title {
  padding: 10px;
  padding-bottom: 18px;
  font-size: 14px;
  line-height: 2em;
  background: #fff;
  border-top: 1px solid #e5e5e5;
}
.User_left_user {
  border-radius: 0 0 3px 3px;
  margin-bottom: 10px;
}
.User_left_name_githud {
  color: #707477;
}
.User_left_name_githud img {
  width: 17px;
  height: 17px;
}
.User_left_avatar {
  width: 40px;
  height: 40px;
  float: left;
  /* margin-right: 10px; */
}
.User_left_name {
  float: left;
  color: #778087;
  font-size: 14px;
  line-height: 2em;
  margin-left: 10px;
}
.User_left_establish {
  margin-bottom: 10px;
}
.User_left_top {
  height: 40px;
  background: #f5f5f5;
  line-height: 40px;
  padding-left: 10px;
  border-radius: 3px 3px 0 0;
}
.User_more {
  color: #666;
  font-size: 14px;
  padding: 10px;
  border-top: 1px solid #f0f0f0;
}
.User_no_article {
  height: 56px;
  color: #2c2c2c;
  padding: 10px;
  background: #fff;
}
.User_left_top_span {
  color: #80bd01;
}
.User_left_top_span:hover {
  text-decoration: underline;
}
.User_left_establish .ArticleList li:hover {
  background: none;
}
.User_left_establish .ArticleList_div_title {
  color: #08c;
}
.User_left_establish .ArticleList_div_title:hover {
  color: rgb(0, 85, 132);
  text-decoration: underline;
}
@media screen and (max-width: 980px) {
  .User_right {
    display: none;
  }
  .User_left {
    width: 100%;
  }
  .head_ul {
    clear: both;
    float: none;
  }
  .User {
    width: 98%;
  }
}
</style>